package com.icloud.cer.rest.i18n;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ErrorCodeHolder {
	private static ErrorCodeHolder instance;
	private static Log log=LogFactory.getLog(ErrorCodeHolder.class);
	//多语言的资源属性名称
	private List<String> bundleNames=new ArrayList<String>();
	private final Map<String, String> data = new ConcurrentHashMap<String, String>();
	
	public static synchronized ErrorCodeHolder getInstance() {
		if (instance == null)
			instance = new ErrorCodeHolder();
		return instance;
	}

	public synchronized void addResourceBundle(String propertiesBaseName){
		I18nUtil.getResourceBundle(Locale.getDefault(), propertiesBaseName);
		log.info("add resource bundle :" + propertiesBaseName);
		bundleNames.add(propertiesBaseName);
	}
	
	
	public String get(String key,Locale locale) {
		for(String rsName: bundleNames){
			ResourceBundle rb=I18nUtil.getResourceBundle(locale, rsName);
			if(rb!=null && rb.containsKey(key)){
				return rb.getString(key);
			}
		}
		return data.get(key);
	}
}
